import { SkuEntity } from '../../transfer/entities/Goods/Output'

export interface IDefaultSku {
    sku: SkuEntity
    values: (string | undefined)[]
}

export default class SkuProcessor {
    // 保存原SKU对象
    private _sku!: SkuEntity[]
    // 定义矩阵对象
    private _matrix: any = {}
    // 分类存放SPEC对象
    private _skuSpecs: any = {}

    constructor(skus: SkuEntity[]) {
        this._sku = skus
        skus.forEach((sku, skuIndex) => {
            // 链表头部指针
            let pMatrixHead = this._matrix
            sku.specs.forEach((spec, index, specs) => {
                // 处理SPEC属性
                if (!this._skuSpecs[spec.specType])
                    // 若SPEC_TYPE不存在，则初始化
                    this._skuSpecs[spec.specType] = {
                        name: spec.specName,
                        values: []
                    }
                // values中不存在该SPEC属性，则添加
                if (this._skuSpecs[spec.specType].values.indexOf(spec.specValue) === -1)
                    this._skuSpecs[spec.specType].values.push(spec.specValue)

                // 构建无向图对应矩阵对象
                if (!pMatrixHead[spec.specValue]) {
                    pMatrixHead[spec.specValue] =
                        index < specs.length - 1
                            ? {}
                            : {
                                  ...sku
                              }
                }
                pMatrixHead = pMatrixHead[spec.specValue]
            })
        })
    }

    /**
     * 获取解析后的SPEC_TYPEs
     */
    public getSpecTypes() {
        return Object.keys(this._skuSpecs)
    }

    /**
     * 获取所有可选属性列表
     */
    public getSpecs() {
        return this._skuSpecs
    }

    public getProcessedSku() {
        return this._matrix
    }
}
